﻿using System.Collections.Generic;
using System.Data;
using personalplaner.business.common;
using personalplaner.business.enums;
using personalplaner.business.stamm;

namespace personalplaner.dataprovider.dienstplan
{
	public partial class SchemaDataService
	{
		private static IEnumerable<PlanSchema> m_planSchemaData(DataTable table)
		{
			var schemaidExists = table.Columns.Contains("schemaid");
			var nameExists = table.Columns.Contains("name");
			var statusExists = table.Columns.Contains("status");

			foreach (DataRow row in table.Rows)
			{
				yield return new PlanSchema
				{
					ID = schemaidExists ? row.Field<int>("schemaid") : int.MaxValue,
					SchemaID = schemaidExists ? row.Field<int>("schemaid") : 0,
					Name = nameExists ? row.Field<string>("name") : string.Empty,
					Status = statusExists ? row.Field<int>("status") == 0 ? enStatus.Inactive : enStatus.Active : enStatus.Inactive
				};
			}
		}

        private static IEnumerable<PlanSchemaItem> m_planSchemaItemsData(DatabaseSettings settings, System.Data.DataTable table)
		{
            var showPrsId = (int)DataServices.OptionsService.NumberValue(settings, enOptions.DisplayOptions, 4) == 1;
            var display = (enNameDisplay)(int)DataServices.OptionsService.NumberValue(settings, enOptions.DisplayOptions, 5);

			var nameExists = table.Columns.Contains("name");
			var schemaidExists = table.Columns.Contains("schemaid");
			var prsschemaidExists = table.Columns.Contains("prsschemaid");
			var prsidExists = table.Columns.Contains("prsid");
			var vornameExists = table.Columns.Contains("vorname");
			var nachnameExists = table.Columns.Contains("nachname");
			var abteilungidExists = table.Columns.Contains("abteilungid");
			var abteilungnameExists = table.Columns.Contains("abteilungname");
			var rgbExists = table.Columns.Contains("rgb");
			var colorExists = table.Columns.Contains("color");

			foreach (DataRow row in table.Rows)
			{
                yield return new PlanSchemaItem
                {

                    Name = nameExists ? row.Field<string>("name") : string.Empty,
                    ID = schemaidExists ? row.Field<int>("schemaid") : int.MaxValue,
                    SchemaID = schemaidExists ? row.Field<int>("schemaid") : 0,
                    PersonSchemaID = prsschemaidExists ? row.Field<int>("prsschemaid") : 0,
                    PrsID = prsidExists ? row.Field<int>("prsid") : 0,
                    Vorname = vornameExists ? row.Field<string>("vorname") : string.Empty,
                    Nachname = nachnameExists ? row.Field<string>("nachname") : string.Empty,
                    AbteilungID = abteilungidExists ? row.Field<int>("abteilungid") : 0,
                    AbteilungName = abteilungnameExists ? row.Field<string>("abteilungname") : string.Empty,
                    RGB = rgbExists ? row.Field<int>("rgb") : 0,
                    ColorString = colorExists ? row.Field<string>("color") : string.Empty,
                    ShowPrsId = showPrsId,
                    NameDisplay = display
                };
			}
		}
	}
}
